package gui;

/*
 * Copyright (c) 1995, 2008, Oracle and/or its affiliates. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 *   - Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *
 *   - Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in the
 *     documentation and/or other materials provided with the distribution.
 *
 *   - Neither the name of Oracle or the names of its
 *     contributors may be used to endorse or promote products derived
 *     from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */ 

/*
 * TableDemo.java requires no other files.
 */

import javax.swing.JFrame;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.AbstractTableModel;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Scanner;
import java.util.Vector;


/** 
 * TableDemo is just like SimpleTableDemo, except that it
 * uses a custom TableModel.
 */
public class TableDemo extends JPanel{
    private boolean DEBUG = false;
    private Vector<String> columnNames;
    private Vector<Lag> data = new Vector<Lag>();
    

    // etiketa textfield eta botoia sortu
    private JLabel label1=new JLabel("Objetu berria");
    private JTextField kuadro1=new JTextField();
    private JButton botoia=new JButton("Txertatu");

    private MyTableModel modeloa = new MyTableModel();
    
    //clase laguntzailea  gero berktorean datuak txertatzeko taulan.
    class Lag {
    	
    	String izena;
    	String abizena;
    	String hobby;
    	Integer zenb;
    	Boolean baiez;
		
    	public Lag(String izena, String abizena, String hobby, Integer zenb,
				Boolean baiez) {
			super();
			this.izena = izena;
			this.abizena = abizena;
			this.hobby = hobby;
			this.zenb = zenb;
			this.baiez = baiez;
		}
    	
    	
    }
    
    public TableDemo()  {
        super(new GridLayout(2,0));
        columnNames=new Vector<String>();
        
        
        JTable table = new JTable(modeloa);
       
        
       
        table.setPreferredScrollableViewportSize(new Dimension(500, 70));
        table.setFillsViewportHeight(true);

        //Create the scroll pane and add the table to it.
        JScrollPane scrollPane = new JScrollPane(table);

        //Add the scroll pane to this panel.
        add(scrollPane);
    }
    
    class MyTableModel extends AbstractTableModel {
     public	 MyTableModel(){
    		addZutabeIzenak();
    		addZutabeBalioak();
    	}
    	
       	public void addZutabeIzenak(){
    		columnNames.add("First Name");
    		columnNames.add("Last Name");
    		columnNames.add("Sport");
    		columnNames.add("# of yers");
    		columnNames.add("Vegetarian");
    	}
    	
    	
       	public void addLerroa(String[] balioak){
       		
       		
       		
       	}
       	
    	public void addZutabeBalioak(){
    	
    		data.add(new Lag("Kathy", "Smith", "Snowboarding", 5, new Boolean(false)));
    		data.add(new Lag("John", "Doe","Rowing", 3, new Boolean(true)));
    		data.add(new Lag("Sue", "Black","Knitting", 2, new Boolean(false)));
    		data.add(new Lag("Jane", "White","Speed reading", new Integer(20), new Boolean(true)));
    		data.add(new Lag("Joe", "Brown","Pool", new Integer(10), new Boolean(false)));
    	}
        public int getColumnCount() {
            return columnNames.size();
        }

        public int getRowCount() {
            return data.size();
        }

        public String getColumnName(int col) {
            return columnNames.elementAt(col);
        }

        public Object getValueAt(int row, int col) {
            
        	
        	switch (col) {
			case 0:
				return data.elementAt(row).izena;
			case 1:
				return data.elementAt(row).abizena;
			case 2:
				return data.elementAt(row).hobby;
			case 3:
				return data.elementAt(row).zenb;
			default:
				return data.elementAt(row).baiez;
			}
        	
        }

        /*
         * JTable uses this method to determine the default renderer/
         * editor for each cell.  If we didn't implement this method,
         * then the last column would contain text ("true"/"false"),
         * rather than a check box.
         */
        public Class getColumnClass(int c) {
            return getValueAt(0, c).getClass();
        }

        /*
         * Don't need to implement this method unless your table's
         * editable.
         */
        public boolean isCellEditable(int row, int col) {
            //Note that the data/cell address is constant,
            //no matter where the cell appears onscreen.
            if (col < 2) {
                return false;
            } else {
                return true;
            }
        }

        /*
         * Don't need to implement this method unless your table's
         * data can change.
         */
        //ADIBIDEA ------->        cespedes,    2,       1
        public void setValueAt(Object value, int row, int col) {
            if (DEBUG) {
                System.out.println("Setting value at " + row + "," + col
                                   + " to " + value
                                   + " (an instance of "
                                   + value.getClass() + ")");
            }

            Lag obj = data.elementAt(row);
        	
            switch (col) {
			case 0:
				obj.izena = (String) value;
			case 1:
				obj.abizena = (String) value;
			case 2:
				obj.hobby = (String) value;
			case 3:
				 obj.zenb = (Integer) value;
			case 4:
				obj.baiez= (Boolean) value;
            }
				
            data.insertElementAt( obj, row);
            fireTableCellUpdated(row, col);//honi deitu barrukoa editatzeko

            if (DEBUG) {
                System.out.println("New value of data:");
                printDebugData();
            }
        }

        private void printDebugData() {
        	//taularen balioak inprimatu
        	for (Lag obj : data) {
				System.out.println(obj);
			}
        	System.out.println("--------------------------");
        }
    }
    /**
     * Create the GUI and show it.  For thread safety,
     * this method should be invoked from the
     * event-dispatching thread.
     */
    private static void createAndShowGUI(){
        //Create and set up the window.
        JFrame frame = new JFrame("TableDemo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        //Create and set up the content pane.
		// newContentPane GRIDLAYOUT bat dauka
        TableDemo newContentPane = new TableDemo();
        newContentPane.setOpaque(true); //content panes must be opaque        
        
        
        newContentPane.sartuEdukia();
        
        
        newContentPane.botoiariListenerraEsleitu();
        
        
        
        
        // Leihoan newContentPane txertatu ( goiko aldean daukagu taula eta beheko aldean gure panela)
        frame.setContentPane(newContentPane);
        
        
        
        //Display the window.
        frame.pack();
        frame.setVisible(true);
    }
   

    private void botoiariListenerraEsleitu() {
        //txertatu botoia
        botoia.addActionListener(new ActionListener() {
       	 			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				lortuTestua();//testua textfield-etik irakurtzeko funtzioa
				
			}

			private void lortuTestua() {
			
		     	String katea = kuadro1.getText();

				String [] balioak=katea.split(",");
				
				modeloa.addLerroa(balioak);
				
			}
		});
		
	}


	private void sartuEdukia() {
        // dena panel batean sartu
    	kuadro1.setColumns(10);
        JPanel panela =new JPanel();
        panela.setLayout(new FlowLayout());// panela flowlayout
        panela.add(label1);
        panela.add(kuadro1);
		panela.add(botoia);
		
		this.add(panela);

		
	}


	public static void main(String[] args) {
        //Schedule a job for the event-dispatching thread:
        //creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
}